package com.meituan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.meituan.mapper.UtilMapper;
import com.meituan.model.ShopEntity;
import com.meituan.mapper.ShopMapper;
import com.meituan.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ShopServiceImpl implements ShopService {
    @Autowired
    ShopMapper shopMapper;
    @Override
    public PageInfo<ShopEntity> findPageAllByMerchantId(String merchantId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ShopEntity> shops = shopMapper.findAllByMerchantIdWithClassify(merchantId);
        PageInfo<ShopEntity> pageInfo = new PageInfo<ShopEntity>(shops);
        return pageInfo;
    }

    @Override
    public List<ShopEntity> findAllByMerchantId(String merchantId) {
        return shopMapper.findAllByMerchantIdWithClassify(merchantId);
    }

    @Override
    public Integer remove(String shopId) {
        return shopMapper.delete(shopId);
    }

    @Override
    public Integer updateShop(ShopEntity shopEntity) {
        return shopMapper.updateShop(shopEntity);
    }

    @Override
    public List<ShopEntity> getAll() {

        return shopMapper.findAllWithDiscount();
    }

    @Override
    public ShopEntity findById(String shopId) {
        return shopMapper.findById(shopId);
    }

    @Override
    public Integer updateShopImg(String shopId, String shopImg) {
        return shopMapper.updateShopImg(shopId,shopImg);
    }

    @Override
    public Integer register(ShopEntity shopEntity) {
        ShopEntity result = shopMapper.findByName(shopEntity.getShopName());
        if(result!=null){
            return 0;
        }
        shopEntity.setRegisterTime(new Date());
        return shopMapper.save(shopEntity);
    }


}
